package com.springbooti18n.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;

/**
 * xufan
 * 2019/1/5
 */
@Configuration
public class InternationalConfig {

    @Value(value = "${spring.messages.basename}")
    private String basename ;

    @Bean(name = "messageSource")
    public ResourceBundleMessageSource getMessageResource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        if(StringUtils.isEmpty(basename)){
            basename = "i18n/message";
        }
        String[] basenames = basename.split(",");
        messageSource.addBasenames(basenames);
        messageSource.setCacheMillis(2);
        return messageSource;
    }

    /**
     *通过自定义的语言器定义系统语言
     * @return
     */
    @Bean
    public LocaleResolver localeResolver(){
        //向容器中添加自定义的 LocaleResolver 对象
        MyLocaleResolver  resolver =  new MyLocaleResolver();
        return resolver;
    }


    @Bean
    public ReloadableResourceBundleMessageSource ReloadableResource(){
        ReloadableResourceBundleMessageSource reloadableResource
                = new ReloadableResourceBundleMessageSource();

        reloadableResource.addBasenames(basename);
        reloadableResource.setCacheMillis(1);
        return  reloadableResource;
    }



}
